var app = getApp()
var quanModel = require("../../model/quanModel.js")
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tags: ["红包优惠券", "折扣优惠券"],
    tagIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  switchTag(e) {
    this.setData({
      tagIndex: e.currentTarget.id,
      quans: getQuan(this.data.list, e.currentTarget.id)
    })
  },
  init() {
    var that = this
    util.showLoading("加载中")
    quanModel.getAllQuan(res => {
      wx.hideLoading()
      that.setData({
        list: res.data,
        quans: getQuan(res.data, 0)
      })
    })
  },
  //优惠券详情
  navToInfo(e){
    app.data.tempQuan=e.currentTarget.dataset.data
    wx.navigateTo({
      url: '../user_quan_info/user_quan_info',
    })
  }

})
//当前显示优惠券列表
function getQuan(list, index) {
  if (index == 0) return getQuan1(list)
  else return getQuan2(list)
}
//红包优惠券
function getQuan1(list) {
  var quans = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].type == 1) quans.push(list[i])
  }
  return quans
}
//折扣优惠券
function getQuan2(list) {
  var quans = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].type == 2) quans.push(list[i])
  }
  return quans
}